<template>
  <div class=''>
    <!-- 面包屑导航区域 -->
    <Bootstrap v-bind:BootstrapTitle="BootstrapTitle"></Bootstrap>
    <!-- 对话框 -->
    <!-- 编辑 -->
    <el-dialog title="编辑分类" :visible.sync="editorDialogVisible" width="35%">
      <div>
        <p>分类名称</p>
        <el-input v-model="editorName.cat_name"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editorDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editorenEnd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- -------------------------------------------------------------------------- -->
    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="35%" @close="endThewind">
      <el-form :model="addCateForm" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称">
          <el-input v-model="addCateForm.cat_name" clearable></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader expandTrigger='hover' v-model="addForm" :options="options" :props="cascaderProps"
            @change="handleChange" clearable change-on-select></el-cascader>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addenEnd">确 定</el-button>
      </span>
    </el-dialog>

    <!-- ------------------------------------------------------------------ -->
    <!-- 对话框 -->
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button size="mini" type="warning" @click="AddClassification">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表单 -->
      <tree-table :data="categoriesData" :columns="columns" :selection-type="false" :expand-type="false" show-index
        index-text="" border>
        <template slot="isOK" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color:lightgreen;"></i>
          <i class="el-icon-error" v-else style="color:lightgreen;"></i>
        </template>
        <!-- 排序 -->
        <template slot="sort" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" size="mini" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="danger" size="mini" v-else="scope.row.cat_level === 2">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="operation" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editorClick(scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteClick(scope.row)">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum" :page-sizes="[5, 10]" :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import Bootstrap from "../../component/component/Bootstrap";
export default {
  name: "",
  components: {
    Bootstrap,
  },
  data() {
    return {
      BootstrapTitle: {
        first: "商品管理",
        second: "/categories",
        third: " 商品分类",
      },
      categoriesData: [],
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      columns: [
        { label: "分类名称", prop: "cat_name" },
        { label: "是否有效", type: "template", template: "isOK" },
        { label: "排序", type: "template", template: "sort" },
        { label: "操作", type: "template", template: "operation" },
      ],
      editorDialogVisible: false,
      addDialogVisible: false,
      editorName: {},
      activeEditorId: "",

      addForm: [],
      options: [],
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      addCateForm: {
        // 分类的等级，默认要添加的是1级分类
        cat_level: 0,
        // 父级分类的Id
        cat_pid: 0,
        // 将要添加的分类的名称
        cat_name: "",
      },
    };
  },
  created() {
    this.getCategoriesList();
  },
  methods: {
    async getCategoriesList() {
      const res = await this.$http.get("categories", { params: this.querInfo });
      // console.log(res, "res");
      if (res.data.meta.status !== 200) {
        return this.$message.error(res.data.meta.msg);
      } else {
        this.categoriesData = res.data.data.result;
        this.total = res.data.data.total;
        this.$message.success(res.data.meta.msg);
      }
    },
    // handleSizeChange
    handleSizeChange(size) {
      // console.log(size, "size");
      this.querInfo.pagesize = size;
      this.getCategoriesList();
    },
    // 监听页数handleCurrentChange
    handleCurrentChange(page) {
      // console.log(page, "page");
      this.querInfo.pagenum = page;
      this.getCategoriesList();
    },
    editorClick(row) {
      this.activeEditorId = row.cat_id;
      // console.log(this.activeEditorId,'this.activeEditorId1');
      this.editorName = row;
      this.editorDialogVisible = true;
    },
    async deleteClick(row) {
      let confirm = await this.$confirm(
        "此操作将永久删除该商品分类, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      if (confirm !== "confirm") {
        this.$message.error("取消删除");
      } else {
        let res = await this.$http.delete("categories/" + row.cat_id);
        if (res.data.meta.status !== 200) {
          return this.$message.error(res.data.meta.msg);
        }
        this.$message.success(res.data.meta.msg);
        this.getCategoriesList();
      }
    },
    async editorenEnd() {
      let name = this.editorName.cat_name;
      // console.log(this.activeEditorId, "activeEditorId");
      let res = await this.$http.put("categories/" + this.activeEditorId, {
        cat_name: name,
      });
      if (res.data.meta.status !== 200) {
        return this.$message.error(res.data.meta.msg);
      }
      this.$message.success(res.data.meta.msg);
      this.editorDialogVisible = false;
      this.getCategoriesList();
      this.activeEditorId = "";
    },
    async AddClassification() {
      this.addDialogVisible = true;
      const res = await this.$http.get("categories", { params: { type: 2 } });
      if (res.data.meta.status !== 200)
        return this.$message.error(res.data.meta.msg);
      this.$message.success(res.data.meta.msg);
      this.options = res.data.data;
    },
    async addenEnd() {
      if (!this.addCateForm.cat_name) return;
      let res = await this.$http.post("categories", this.addCateForm);
      if (res.data.meta.status !== 201) {
        this.$message.error(res.data.meta.msg);
      } else {
        console.log(123);
        this.$message.success(res.data.meta.msg);
        this.addDialogVisible = false;
        this.getCategoriesList();
      }
    },
    handleChange() {
      // 如果 selectedKeys 数组中的 length 大于0，证明选中的父级分类
      // 反之，就说明没有选中任何父级分类
      if (this.addForm.length > 0) {
        // 父级分类的Id
        this.addCateForm.cat_pid = this.addForm[this.addForm.length - 1];
        // 为当前分类的等级赋值
        this.addCateForm.cat_level = this.addForm.length;
      } else {
        // 父级分类的Id
        this.addCateForm.cat_pid = 0;
        // 为当前分类的等级赋值
        this.addCateForm.cat_level = 0;
      }
    },
    // 监听对话框关闭endThewind
    endThewind() {
      this.addForm = [];
      this.addCateForm.cat_level = 0;
      this.addCateForm.cat_pid = 0;
    },
  },
};
</script>
<style lang='less' scoped>
.el-cascader {
  width: 100%;
}
</style>